<?php
namespace app\console\controller;
use app\model\MemberIntegral as SelfModel;
use app\model\Member;

use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\facade\Route;
use think\facade\View;
use think\facade\Db;
use think\facade\Cache;
use think\facade\Cookie;
class MemberIntegral extends Base
{



    public function initialize()
    {
        parent::initialize();
        $this->model = "\app\model\MemberIntegral";
        $this->list_rows = 20;
        
    }
    public function select()
    {
            $member = Member::where([['status', '=', 1]])->order(['sort' => 'asc', 'id' => 'desc'])->select()->toArray();
            View::assign('member', $member);

    }
    public function export($filename='1.xlsx')
    {
        $filename = '积分记录导出';
        $param = $this->request->param();
        if(!empty($param['create_time'])){
            $filename .= ' '.$param['create_time'];
        }
        $filename .= '.xlsx';
        $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $header = ['ID','订单号','类型','会员','符号','积分值','操作前','操作后','创建时间'];
        foreach ($header as $kh => $vh){
            $sheet->setCellValueByColumnAndRow($kh+1,1, $vh);
        }
        $data = SelfModel::withSearch(array_keys($param), $param)->lock(true)->order(['id'=>'desc'])->select();
        foreach ($data as $k => $v){
            $sheet->setCellValueByColumnAndRow(1,1+$k+1, $v['id']);
            $sheet->setCellValueExplicitByColumnAndRow(2,1+$k+1, $v['content'],DataType::TYPE_STRING);
            $sheet->setCellValueByColumnAndRow(3,1+$k+1, $v['type']);
//            $sheet->setCellValueExplicitByColumnAndRow(4,1+$k+1, $v['withdraw_no'],DataType::TYPE_STRING);
            $sheet->setCellValueExplicitByColumnAndRow(4,1+$k+1, $v['member_mobile'],DataType::TYPE_STRING);
            $sheet->setCellValueByColumnAndRow(5,1+$k+1, $v['mark']);
            $sheet->setCellValueByColumnAndRow(6,1+$k+1, $v['integral']);
            $sheet->setCellValueByColumnAndRow(7,1+$k+1, $v['before_integral']);
            $sheet->setCellValueByColumnAndRow(8,1+$k+1, $v['after_integral']);
            $sheet->setCellValueByColumnAndRow(9,1+$k+1, $v['create_time']);


        }
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$filename.'"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');
        // If you're serving to IE over SSL, then the following may be needed
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0

        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        $spreadsheet->disconnectWorksheets();
        unset($spreadsheet);
        ob_end_flush();

    }



}